package com.lorianknights.rpg.ui
{
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import com.lorianknights.rpg.ui.templates.UITemplate;
	import com.lorianknights.rpg.utils.Constants;
	import com.lorianknights.rpg.ui.skins.UIAchievementSkin;
	import com.lorianknights.rpg.managers.AchievementManager;

	public class UIAchievement extends UITemplate
	{
		
		private var achData:Achievement;
		private var skin:UIAchievementSkin;
		public static const ACH_RECEIVED:String = Constants.ACHIEVEMENT_RECEIVED_STR;
		public var ldr:Loader = new Loader();
		
		public function UIAchievement()
		{
			
			super();
		
		}
		
		public function setAchievement(data:Object, achID:int):void
		{
			
			this.skin.strTitle_txt.text = AchievementManager.getAchievementTitleByID(data[achID]);
			this.skin.strDesc_txt.text = AchievementManager.getAchievementDescriptionByID(data[achID]);
			this.skin.endRes_txt.text = this.ACH_RECEIVED;
			this.ldr.load(new URLRequest(Game.BASE_URL + Constants.ACHIEVEMENT_DIRECTORY + AchievementManager.getAchievementFileByID(data[achID]);
			this.ldr.addEventListener(Event.COMPLETE, onAchievementImageLoaded);
			this.ldr.addEventListener(IOErrorEvent.IO_ERROR, onError);
			
		}
		
		private function onError(event:IOErrorEvent):void
		{
			
			Utility.traceMessage("achievement img load error: " + event);
			
		}
	
		private function onAchievementImageLoaded(event:Event):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			this.skin.imgHolderMC.addChild(mc);
			
		}
		
		public override function destroy():void
		{
			
			this.skin = null;
			this.achData = null;
			this.ldr.unload();
			removeChild(this.skin);
			
		}
		
		private function init(event:Event):void
		{
			
			this.skin = new AchievementMCSkin();
			addChild(this.skin);
			
				if (this.skin == null)
				{
					
					assetClass = getDefinitionByName(Constants.ACHIEVEMENT_UI_SKIN) as Class;
					this.skin.strTitle_txt = this.skin.getChildByName(Constants.STR_TITLE_TXT) as TextField;
					this.skin.strDesc_txt = this.skin.getChildByName(Constants.STR_DESC_TXT) as TextField;
					this.skin.endRes_txt = this.skin.getChildByName(Constants.STR_END_RES_TXT) as TextField;
					this.skin.uniqueImgMC = this.skin.getChildByName(Constants.STR_UNIQUE_MC) as MovieClip;
					this.skin = addChildAt(new assetClass(), 0) as MovieClip;
					
						if (this.skin.numChildren > 0)
						{
							
							addChild(this.skin);
							
						}
				}
				
				this.skin.strTitle_txt.text = Constants.PRE_ACHIEVEMENT_TITLE;
				this.skin.strDesc_txt.text = Constants.PRE_ACHIEVEMENT_DESC;
				this.skin.endRes_txt.text = this.ACH_RECEIVED;
				
		}
	}
}
				